#include "msgpaneleditor.h"
#include <memory>
MsgPanelEditor::MsgPanelEditor(QWidget *parent) : QDialog(parent)
{
    m_PanelName.clear();
    m_PanelName<<"系统查找"<<"通信协议编辑"<<"消息数据编辑";
    m_EditType = NOTTODO;
}

MsgPanelEditor::~MsgPanelEditor()
{

}

VOID MsgPanelEditor::Init()
{
}

VOID MsgPanelEditor::CreatePanelByType(PanelType eType,EditType eEType,const QList<QTableWidgetItem*>* pItems)
{
    switch (eType)
    {
    case SYSPANEL:
    {
        CreateSysEditorPanel(eEType,pItems);
        break;
    }
     case SUBPANEL:
    {
        CreateSubEditorPanel(eEType,pItems);
        break;
    }
    case SUBDATAPANEL:
    {
        CreateSubDataEditorPanel(eEType,pItems);
        break;
    }
    case FINDPANEL:
    {
        CreateFindPanel();
        break;
    }
    }
}

VOID MsgPanelEditor::CreateSysEditorPanel(EditType eType,const QList<QTableWidgetItem*>* pItems)
{

    QString sTitleName = "";
    QString sIcoPaht = "";
    switch (eType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加系统";
        sIcoPaht =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑系统";
        sIcoPaht = ":/Icon/Edit.png";
        break;
    }
    default:
        break;
    }

    m_FindDialog = new QDialog();
    m_FindDialog->setWindowTitle(tr("系统编辑"));
    m_FindDialog->setWindowIcon(QIcon(sIcoPaht));
    m_FindDialog->setWindowTitle(sTitleName);


    QFrame* mainFrame = new QFrame(m_FindDialog);

    QFrame* inBackGroundFrame = new QFrame(mainFrame);
    inBackGroundFrame->setFrameShape(QFrame::WinPanel);
    inBackGroundFrame->setFrameShadow(QFrame::Sunken);
    inBackGroundFrame->setLineWidth(3);
    inBackGroundFrame->setMidLineWidth(2);
    QFrame* inputIdFrame = new QFrame(inBackGroundFrame);
    QLabel* sysIdLable = new QLabel(inputIdFrame);
    sysIdLable->setText("系统ID号:");
    m_SysInput = new QLineEdit(inputIdFrame);
    m_SysInput->setFixedSize(250,30);
    m_SysInput->setValidator(new QIntValidator(0,1000,this));
    QHBoxLayout* inputIdLayout = new QHBoxLayout(inputIdFrame);
    inputIdLayout->addWidget(sysIdLable);
    inputIdLayout->addWidget(m_SysInput);
    inputIdFrame->setLayout(inputIdLayout);

    QFrame* inputDataFrame = new QFrame(inBackGroundFrame);
    QLabel* sysDataLable = new QLabel(inputDataFrame);
    sysDataLable->setText("系统名称:");
    m_SysInputData = new QLineEdit(inputDataFrame);
    m_SysInputData->setFixedSize(250,30);
    QHBoxLayout* inputDataLayout = new QHBoxLayout(inputDataFrame);
    inputDataLayout->addWidget(sysDataLable);
    inputDataLayout->addWidget(m_SysInputData);
    inputDataFrame->setLayout(inputDataLayout);


    QVBoxLayout* InputLayout = new QVBoxLayout(inBackGroundFrame);
    InputLayout->addWidget(inputIdFrame);
    InputLayout->addWidget(inputDataFrame);
    inBackGroundFrame->setLayout(InputLayout);

    QFrame* btnFrame = new QFrame(mainFrame);
    QPushButton * m_OkBtn = new QPushButton(btnFrame);
    m_OkBtn->setText("确定");
    m_OkBtn->setFixedSize(80,30);
    QPushButton * m_CancelBtn = new QPushButton(btnFrame);
    m_CancelBtn->setText("退出");
    m_CancelBtn->setFixedSize(80,30);
    QSpacerItem* spLeft = new QSpacerItem(55,20,QSizePolicy::Minimum, QSizePolicy::Fixed);
    QSpacerItem* spRight = new QSpacerItem(55,20,QSizePolicy::Minimum, QSizePolicy::Fixed);

    QHBoxLayout* btnLayout = new QHBoxLayout(btnFrame);
    btnLayout->addSpacerItem(spLeft);
    btnLayout->addWidget(m_OkBtn);
    btnLayout->addWidget(m_CancelBtn);
    btnLayout->addSpacerItem(spRight);
    btnFrame->setLayout(btnLayout);

    QVBoxLayout* backGroundLayout = new QVBoxLayout(mainFrame);
    backGroundLayout->addWidget(inBackGroundFrame);
    backGroundLayout->addWidget(btnFrame);
    mainFrame->setLayout(backGroundLayout);

    QLabel* titleLabel = new QLabel(m_FindDialog);
    titleLabel->setText(sTitleName);
    QFont    font ( "仿宋",  12,   75);
    titleLabel->setFont(font);
    titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    QVBoxLayout* mainLayout = new QVBoxLayout(m_FindDialog);
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(mainFrame);
    mainLayout->setContentsMargins (40,5,40,3);
    mainLayout->setSpacing(2);
    m_FindDialog->setLayout(mainLayout);

    m_FindDialog->setFixedSize(420,230);
    m_FindDialog->setAttribute(Qt::WA_DeleteOnClose);

    if(eType == EDIT_DATA)
    {
        if(nullptr != pItems)
        {
            m_SysInput->setText(pItems->at(0)->text());
            QString sData =pItems->at(1)->text();
            sData = sData.mid(2,sData.size()-4);
            m_SysInputData->setText(sData);
        }
    }
    m_EditType = eType;
    connect(m_OkBtn,&QPushButton::clicked,this,
            [&]{
        onFindOkBtnClick(SYSPANEL);
    });
    connect(m_CancelBtn,&QPushButton::clicked,this,&MsgPanelEditor::onFindCancelBtnClick);
    m_FindDialog->exec();
}

VOID MsgPanelEditor::CreateSubEditorPanel(EditType eType,const QList<QTableWidgetItem*>* pItems)
{
    QString sTitleName = "";
    QString sIconPath = "";
    switch (eType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加协议";
        sIconPath =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑协议";
         sIconPath =":/Icon/Edit.png";
        break;
    }
    default:
        break;
    }

    m_FindDialog = new QDialog();
    m_FindDialog->setWindowTitle(tr("协议编辑"));
    m_FindDialog->setWindowIcon(QIcon(sIconPath));
    m_FindDialog->setWindowTitle(sTitleName);

    QFrame* mainFrame = new QFrame(m_FindDialog);

    QFrame* inBackGroundFrame = new QFrame(mainFrame);
    inBackGroundFrame->setFrameShape(QFrame::WinPanel);
    inBackGroundFrame->setFrameShadow(QFrame::Sunken);
    inBackGroundFrame->setLineWidth(3);
    inBackGroundFrame->setMidLineWidth(2);

    QFrame* inputIdFrame = new QFrame(inBackGroundFrame);
    QLabel* sysIdLable = new QLabel(inputIdFrame);
    sysIdLable->setText("协议ID号:");
    m_SysInput = new QLineEdit(inputIdFrame);
    m_SysInput->setFixedSize(250,30);
    m_SysInput->setValidator(new QIntValidator(0,1000,this));
    QHBoxLayout* inputIdLayout = new QHBoxLayout(inputIdFrame);
    inputIdLayout->addWidget(sysIdLable);
    inputIdLayout->addWidget(m_SysInput);
    inputIdFrame->setLayout(inputIdLayout);

    QFrame* inputDataFrame = new QFrame(inBackGroundFrame);
    QLabel* sysDataLable = new QLabel(inputDataFrame);
    sysDataLable->setText("协议名称:");
    m_SysInputData = new QLineEdit(inputDataFrame);
    m_SysInputData->setFixedSize(250,30);
    QHBoxLayout* inputDataLayout = new QHBoxLayout(inputDataFrame);
    inputDataLayout->addWidget(sysDataLable);
    inputDataLayout->addWidget(m_SysInputData);
    inputDataFrame->setLayout(inputDataLayout);

    QFrame* subMsgTypeFrame = new QFrame(inBackGroundFrame);
    QLabel* subMsgTypeLable = new QLabel(subMsgTypeFrame);
    subMsgTypeLable->setText("协议来源:");
    m_SubType = new QComboBox(subMsgTypeFrame);
    m_SubType->clear();
    m_SubType->addItems(QStringList()<<"服务器"<<"客户端");
    m_SubType->setView(new QListView());
    m_SubType->setFixedSize(70,28);
    QSpacerItem* spMsgLeft = new QSpacerItem(200,20,QSizePolicy::Minimum, QSizePolicy::Fixed);
    QHBoxLayout* subMsgTypeLayout = new QHBoxLayout(subMsgTypeFrame);
    subMsgTypeLayout->addWidget(subMsgTypeLable);
    subMsgTypeLayout->addSpacerItem(spMsgLeft);
    subMsgTypeLayout->addWidget(m_SubType);
    subMsgTypeFrame->setLayout(subMsgTypeLayout);

    QVBoxLayout* InputLayout = new QVBoxLayout(inBackGroundFrame);
    InputLayout->addWidget(inputIdFrame);
    InputLayout->addWidget(inputDataFrame);
    InputLayout->addWidget(subMsgTypeFrame);
    inBackGroundFrame->setLayout(InputLayout);

    QFrame* btnFrame = new QFrame(mainFrame);
    QPushButton * m_OkBtn = new QPushButton(btnFrame);
    m_OkBtn->setText("确定");
    m_OkBtn->setFixedSize(80,30);
    QPushButton * m_CancelBtn = new QPushButton(btnFrame);
    m_CancelBtn->setText("退出");
    m_CancelBtn->setFixedSize(80,30);
    QSpacerItem* spLeft = new QSpacerItem(55,20,QSizePolicy::Minimum, QSizePolicy::Fixed);
    QSpacerItem* spRight = new QSpacerItem(55,20,QSizePolicy::Minimum, QSizePolicy::Fixed);

    QHBoxLayout* btnLayout = new QHBoxLayout(btnFrame);
    btnLayout->addSpacerItem(spLeft);
    btnLayout->addWidget(m_OkBtn);
    btnLayout->addWidget(m_CancelBtn);
    btnLayout->addSpacerItem(spRight);
    btnFrame->setLayout(btnLayout);

    QVBoxLayout* backGroundLayout = new QVBoxLayout(mainFrame);
    backGroundLayout->addWidget(inBackGroundFrame);
    backGroundLayout->addWidget(btnFrame);
    mainFrame->setLayout(backGroundLayout);

    QLabel* titleLabel = new QLabel(m_FindDialog);
    titleLabel->setText(sTitleName);
    QFont    font ( "仿宋",  12,   75);
    titleLabel->setFont(font);
    titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    QVBoxLayout* mainLayout = new QVBoxLayout(m_FindDialog);
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(mainFrame);
    mainLayout->setContentsMargins (40,5,40,1);
    mainLayout->setSpacing(5);
    m_FindDialog->setLayout(mainLayout);

    m_FindDialog->setFixedSize(430,250);
    m_FindDialog->setAttribute(Qt::WA_DeleteOnClose);


    if(eType == EDIT_DATA)
    {
        if(nullptr != pItems)
        {
            m_SysInput->setText(pItems->at(0)->text());
            m_SysInputData->setText(pItems->at(1)->text());
            m_SubType->setCurrentIndex(pItems->at(2)->type());
        }
    }
    m_EditType = eType;
    connect(m_OkBtn,&QPushButton::clicked,this,
            [&]{
        onFindOkBtnClick(SUBPANEL);
    });
    connect(m_CancelBtn,&QPushButton::clicked,this,&MsgPanelEditor::onFindCancelBtnClick);

    m_FindDialog->exec();
}

VOID MsgPanelEditor::CreateSubDataEditorPanel(EditType eEType,const QList<QTableWidgetItem*>* pItems)
{
    qDebug()<<(INT)eEType;

    QString sTitleName = "";
    QString sIconPath = "";
    switch (eEType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加数据";
        sIconPath =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑数据";
         sIconPath =":/Icon/Edit.png";
        break;
    }
    default:
        break;
    }

    m_FindDialog = new QDialog();
    m_FindDialog->setWindowTitle(tr("消息编辑"));
    m_FindDialog->setWindowIcon(QIcon(sIconPath));
    m_FindDialog->setWindowTitle("消息编辑");

    QLabel* titleLabel = new QLabel(m_FindDialog);
    titleLabel->setText(sTitleName);
    QFont    font ( "仿宋",  12,   75);
    titleLabel->setFont(font);
    titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    QFrame* inputFram = new QFrame(m_FindDialog);
    QVBoxLayout* inputLatour = new QVBoxLayout(inputFram);
    inputFram->setFrameShape(QFrame::WinPanel);
    inputFram->setFrameShadow(QFrame::Sunken);
    inputFram->setLineWidth(3);
    inputFram->setMidLineWidth(2);

    QFrame* fromFrame = new QFrame();
    QLabel* fromLabel = new QLabel(fromFrame);
    fromLabel->setText("消息类型:");
    m_MsgBox = new QComboBox(inputFram);
    m_MsgBox->clear();
    m_MsgBox->addItems(GetMsgTypeList());
    m_MsgBox->setView(new QListView());
    m_MsgBox->setFixedSize(130,28);

    QFrame* dataFrame = new QFrame(inputFram);
    QLabel* dataLabel = new QLabel(dataFrame);
    dataLabel->setText("消息描述:");
    m_MsgInputData = new QLineEdit(dataFrame);
    m_MsgInputData->setFixedSize(250,30);
    QHBoxLayout* dataLayout = new QHBoxLayout(dataFrame);
    dataLayout->addWidget(dataLabel);
    dataLayout->addWidget(m_MsgInputData);
    dataFrame->setLayout(dataLayout);


    QHBoxLayout* fromLayout = new QHBoxLayout(fromFrame);
    fromLayout->addWidget(fromLabel);
    fromLayout->addWidget(m_MsgBox);
    fromFrame->setLayout(fromLayout);

    inputLatour->addWidget(fromFrame);
    inputLatour->addWidget(dataFrame);
    inputFram->setLayout(inputLatour);


    QGroupBox* msgGroup = new QGroupBox(m_FindDialog);
    QGridLayout* textIptLayout = new QGridLayout(msgGroup);
    msgGroup->setTitle("数据描述");
    m_MsgDataEdit = new QTextEdit(msgGroup);
    m_MsgDataEdit->clear();
    m_MsgDataEdit->setFont(QFont(tr("仿宋"), 12));
    textIptLayout->addWidget(m_MsgDataEdit);
    msgGroup->setLayout(textIptLayout);

    QFrame* btnFrame = new QFrame(m_FindDialog);
    m_OkBtn = new QPushButton(btnFrame);
    m_OkBtn->setText("确定");
    m_OkBtn->setFixedSize(80,30);
    m_CancelBtn = new QPushButton(btnFrame);
    m_CancelBtn->setText("退出");
    m_CancelBtn->setFixedSize(80,30);
    QSpacerItem* spLet = new QSpacerItem(150,20,QSizePolicy::Minimum, QSizePolicy::Fixed);
    QHBoxLayout* btnLayout = new QHBoxLayout(btnFrame);
    btnLayout->addSpacerItem(spLet);
    btnLayout->addWidget(m_OkBtn);
    btnLayout->addWidget(m_CancelBtn);
    btnFrame->setLayout(btnLayout);

    QVBoxLayout* mainLayout = new QVBoxLayout(m_FindDialog);
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(inputFram);
    mainLayout->addWidget(msgGroup);
    mainLayout->addWidget(btnFrame);
    mainLayout->setSpacing(2);
    m_FindDialog->setLayout(mainLayout);

    m_FindDialog->setFixedSize(370,420);
    m_FindDialog->setAttribute(Qt::WA_DeleteOnClose);

    if(eEType == EDIT_DATA)
    {
        if(nullptr != pItems)
        {

            m_MsgBox->setCurrentIndex(pItems->at(0)->type());
            m_MsgInputData->setText(pItems->at(1)->text());
            m_MsgDataEdit->setText(m_PanelString);
        }
    }
    m_EditType = eEType;
    connect(m_OkBtn,&QPushButton::clicked,this,
            [&]{
        onFindOkBtnClick(SUBDATAPANEL);
    });
    connect(m_CancelBtn,&QPushButton::clicked,this,&MsgPanelEditor::onFindCancelBtnClick);
    m_FindDialog->exec();
}

VOID MsgPanelEditor::CreateFindPanel()
{
    m_FindDialog = new QDialog();
    m_FindDialog->setWindowTitle(tr("数据查找"));
    m_FindDialog->setWindowIcon(QIcon(":/Icon/find.png"));
    m_FindDialog->setWindowTitle(m_PanelName.at(0));
    QWidget* findWindow = new QWidget(m_FindDialog);

    QGroupBox* findBox = new QGroupBox(findWindow);
    findBox->setTitle("数据查找");
    QWidget* inputW = new QWidget(findBox);
    QHBoxLayout* inputL = new QHBoxLayout(inputW);

    m_InptuLabel = new QLabel(tr("输入内容:"),inputW);
    m_LineEditor = new QLineEdit(inputW);
    inputL->addWidget(m_InptuLabel);
    inputL->addWidget(m_LineEditor);
    inputW->setLayout(inputL);
    m_LineEditor->setPlaceholderText("只检测两个以上连续字符不区分大小写...");
    m_ProgerssBar = new QProgressBar(findBox);

    QVBoxLayout* findL = new QVBoxLayout(findBox);
    findL->addWidget(inputW);
    findL->addWidget(m_ProgerssBar);
    findBox->setLayout(findL);

    QWidget* btnW = new QWidget(findWindow);
    m_OkBtn = new QPushButton(tr("查找"),btnW);
    m_CancelBtn = new QPushButton(tr("退出"),btnW);
    m_checkBox = new QCheckBox(btnW);
    m_checkBox->setText("全字符匹配");
    QVBoxLayout* btnL = new QVBoxLayout(btnW);
    btnL->addWidget(m_OkBtn);
    btnL->addWidget(m_CancelBtn);
    btnL->addWidget(m_checkBox);
    btnW->setLayout(btnL);

    QHBoxLayout* findWindowL = new QHBoxLayout(findWindow);
    findWindowL->addWidget(findBox);
    findWindowL->addWidget(btnW);
    findWindow->setLayout(findWindowL);

    m_TreeBox = new QGroupBox(m_FindDialog);
    m_TreePanel = new QTreeWidget(m_TreeBox);
    QStringList sHeaderList;
    sHeaderList<<"<<系统id>"<<"<数据信息>"<<"<子协议类型>"<<"";
    m_TreePanel->setHeaderLabels(sHeaderList);

    QVBoxLayout* treeL = new QVBoxLayout(m_TreeBox);
    treeL->addWidget(m_TreePanel);
    m_TreeBox->setLayout(treeL);

    QVBoxLayout* mainL = new QVBoxLayout();
    mainL->addWidget(findWindow);
    mainL->addWidget(m_TreeBox);
    m_FindDialog->setLayout(mainL);

    m_ProgerssBar->setOrientation(Qt::Horizontal);
    m_ProgerssBar->setMinimum(0);
    m_ProgerssBar->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_FindDialog->resize(450,600);

    m_FindDialog->setAttribute(Qt::WA_DeleteOnClose);   //关闭释放 55号

    connect(m_OkBtn,&QPushButton::clicked,this,
            [&]{
        onFindOkBtnClick(FINDPANEL);
    });
    connect(m_CancelBtn,&QPushButton::clicked,this,&MsgPanelEditor::onFindCancelBtnClick);
    m_FindDialog->exec();
}


VOID MsgPanelEditor::UpdateFindProgerssBar(INT nCurProgerss, INT nMaxProgress,INT nFindNum)
{
    if(nullptr != m_ProgerssBar)
    {
        m_ProgerssBar->setMaximum(nMaxProgress);
        m_ProgerssBar->setValue(nCurProgerss);
        INT nCurPro = (nCurProgerss/nMaxProgress)*100;
        m_ProgerssBar->setFormat(tr("查找进度 : %1").arg(nCurPro));
        m_TreeBox->setTitle("查找到["+QString::number(nFindNum)+"]条数据");
        if(nCurProgerss == nMaxProgress)
        {
            ShowFindData();
        }
    }
}

VOID MsgPanelEditor::ShowFindData()
{
    if(m_FindData.isEmpty())
    {
        m_TreePanel->clear();
        return;
    }

    m_TreePanel->clear();

    for(auto iter = m_FindData.begin();iter != m_FindData.end();++iter)
    {
        QTreeWidgetItem* SysItem = new QTreeWidgetItem();
        if(iter->nSysId < 0)    continue;
        SysItem->setText(0,"系统ID:["+QString::number(iter->nSysId)+"]");
        SysItem->setText(1,iter->sMsg);

        auto SubData =iter->m_SubVector;
        for(INT i = 0;i<SubData.size();++i)
        {
            QTreeWidgetItem* SubItem = new QTreeWidgetItem();
            SubItem->setText(0,"协议ID:["+QString::number(SubData[i].nSubId)+"]");
            SubItem->setText(1,SubData[i].sSubData);
            SubItem->setFirstColumnSpanned(true);
            if(SubData[i].nSubId < 0 )continue;

            QString sMsgType = "";
            switch (SubData[i].eType)
            {
            case SERVER:
            {
                sMsgType = "服务器";
                SubItem->setTextColor(0,QColor(0,0,139));
                SubItem->setTextColor(1,QColor(0,0,139));
                SubItem->setTextColor(2,QColor(0,0,139));
                const QColor color = QColor(252,222,156);
                SubItem->setBackground(2,QBrush(color));
                break;
            }
            case CLIENT:
                sMsgType = "客户端";
                break;
            }
            SubItem->setText(2,sMsgType);
            SysItem->addChild(SubItem);
        }
        m_TreePanel->addTopLevelItem(SysItem);
    }
}


void MsgPanelEditor::onFindOkBtnClick(PanelType ePanelType)
{

    switch (ePanelType)
    {
        case SYSPANEL:
        {
            if(m_SysInput->text().isEmpty() || m_SysInputData->text().isEmpty())
            {
                m_FindDialog->close();
                break;
            }
            emit sendSysEditorBtnClick(m_SysInput->text().toInt(),m_SysInputData->text(),m_EditType);
            m_FindDialog->close();
            break;
        }
        case SUBPANEL:
        {
            if(m_SysInput->text().isEmpty() || m_SysInputData->text().isEmpty())
            {
                m_FindDialog->close();
                break;
            }
            emit sendSubEditorBtnClick(m_SysInput->text().toInt(),m_SysInputData->text(),m_SubType->currentIndex(),m_EditType);
            m_FindDialog->close();
            break;
        }
        case SUBDATAPANEL:
        {
            if(m_MsgInputData->text().isEmpty())
            {
                m_FindDialog->close();
                return;
            }

            emit sendSubDataEditorBtnClick(m_MsgBox->currentIndex(),m_MsgInputData->text(),m_MsgDataEdit->toPlainText(),m_EditType);
            m_FindDialog->close();
            break;
        }
        case FINDPANEL:
        {
            if(m_LineEditor->text().isEmpty()) break;
            emit sendFindOkBtnClick(m_LineEditor->text(),(INT)m_checkBox->checkState());
            break;
        }
    }
}

void MsgPanelEditor::onFindCancelBtnClick()
{
    m_FindDialog->close();
}
